// pages/comment/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userProEvalu: 3,
    userSerEvalu: 3,
    lineProEvalu: 3,
    lineSerEvalu: 3,
    userContent: '',
    line_content: '',
    isChose: false,
    lineId: '',
    orderId: '',
    meetUids: [],
    bayUids: [],
    allUser: [],
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.lineId = options.lineId;
    this.data.orderId = options.orderId;
    util.http({
      url: 'api/miniwechat/index/evals',
      data: {
        line_id: this.data.lineId,
        order_id: this.data.orderId
      }
    }).then((data) => {
      this.setData({
        lineInfo: data
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
    util.http({
      url: 'api/miniwechat/index/partner',
      data: {
        line_id: this.data.lineId,
        user_id: util.isLogin().user_id
      }
    }).then((data) => {
      this.setData({
        allUser: data
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  changePro(e) {
    let level = e.currentTarget.dataset.level;
    this.setData({
      userProEvalu: level
    })
  },
  changeSer(e) {
    let level = e.currentTarget.dataset.level;
    this.setData({
      userSerEvalu: level
    })
  },
  changeLPro(e) {
    let level = e.currentTarget.dataset.level;
    this.setData({
      lineProEvalu: level
    })
  },
  changeLSer(e) {
    let level = e.currentTarget.dataset.level;
    this.setData({
      lineSerEvalu: level
    })
  },
  lineInput(e) {
    this.setData({
      lineContent: e.detail.value
    })
  },
  userInput(e) {
    this.setData({
      userContent: e.detail.value
    })
  },
  userChose(e) {
    console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        isChose: true,
        type: 1
      })
    } else {
      this.setData({
        isChose: true,
        type: 0
      })
    }
  },
  userClose(e) {
    this.setData({
      isChose: false
    })
  },
  addUser(e) {
    let userId = e.currentTarget.dataset.userid;
    let allIndex = this.data.allUser.findIndex(item => {
      return item.id == userId
    })
    if (this.data.allUser[allIndex].isChose) {
      this.data.allUser[allIndex].isChose = false
    } else {
      this.data.allUser[allIndex].isChose = true
    }
    this.setData({
      allUser: this.data.allUser
    })
  },
  submitPro() {
    if (this.data.type == 1) {
      this.data.allUser.forEach((item, index) => {
        if (item.isChose) {
          this.data.meetUids.push(item)
        }
      })
      this.setData({
        meetUids: this.data.meetUids
      });
      this.data.meetUids.forEach((item,index)=>{
        let i = this.data.allUser.findIndex(sItem => {
          return sItem.id == item.id
        })
        if(i!==-1) {
          this.data.allUser.splice(i,1);
        }
      })
      this.setData({
        allUser: this.data.allUser,
        isChose: false
      });
    } else {
      this.data.allUser.forEach((item, index) => {
        if (item.isChose) {
          this.data.bayUids.push(item)
        }
      })
      this.setData({
        bayUids: this.data.bayUids
      })
      this.data.bayUids.forEach((item,index)=>{
        let i = this.data.allUser.findIndex(sItem => {
          return sItem.id == item.id
        })
        if(i!==-1) {
          this.data.allUser.splice(i,1);
        }
      })
      this.setData({
        allUser: this.data.allUser,
        isChose: false
      });
    }
  },
  delUser(e) {
    let userId = e.currentTarget.dataset.userid;
    if(e.currentTarget.dataset.type == 1) {
      let index = this.data.meetUids.findIndex(item => {
        return item.id == userId
      })
      if(index !== -1) {
        let it = this.data.meetUids.splice(index,1)
        console.log(it)
        this.data.allUser.push(it[0]);
      }
      this.setData({
        meetUids: this.data.meetUids
      });
    } else {
      let index = this.data.bayUids.findIndex(item => {
        return item.id == userId
      })
      if(index !== -1) {
        let it = this.data.bayUids.splice(index,1)
        console.log(it)
        this.data.allUser.push(it[0]);
      }
      this.setData({
        bayUids: this.data.bayUids
      });
    }
    this.setData({
      allUser: this.data.allUser
    });
    
  },
  submit(e) {
    let meet_uids = [];
    this.data.meetUids.forEach(item=>{
      meet_uids.push(item.id);
    })
    let bay_uids = [];
    this.data.bayUids.forEach(item=>{
      bay_uids.push(item.id);
    })
    util.http({
      url: 'api/miniwechat/index/evals',
      method: 'POST',
      data: {
        user_pro_evalu: this.data.userProEvalu,
        user_ser_evalu: this.data.userSerEvalu,
        user_content: this.data.userContent,
        line_pro_evalu: this.data.lineProEvalu,
        lnie_ser_evalu: this.data.lineSerEvalu,
        line_content: this.data.lineContent,
        meet_uids: meet_uids,
        bay_uids: bay_uids,
        user_id: util.isLogin().user_id,
        order_id: this.data.orderId,
        line_id: this.data.lineId
      }
    }).then((data) => {
      wx.showToast({
        title: '发送成功',
        icon: 'none',
        duration: 1000
      })
      setTimeout(() =>{
        wx.navigateBack({
          delta: 1
        })
      },1000)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  }
})